package data;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;

import org.jdom.Element;

import fileIO.EmailNoticeWriter;
import fileIO.XMLParser;


/**
 * Retrieves employee data from stored XML files on launch
 * @author lax116
 */
public class EmailReminder {
	private static HashMap<String,ArrayList<String>> reminderMap;
	
	public static void initialize(){
		reminderMap = new HashMap<String, ArrayList<String>>();
		
		Element root = null;
		//FilePath.CS_LIST
		root = XMLParser.parseDocument(FilePath.REMINDER_LIST);
		if(root==null){
			JOptionPane.showMessageDialog(null, "CS List could not be opened at: " + FilePath.REMINDER_LIST +". "+ "Create an employee to save a new file");
		}else{
			parseList(root);
		}
	}
	private static void parseList(Element root){
		Object[] childList = root.getChildren().toArray();
		//Iterate through list of locations
		for(int i =0;i<childList.length;i++){
			Element currElement = (Element)childList[i];
			ArrayList<String> list = new ArrayList<String>();
			String keyStr = currElement.getAttributeValue("Key").toUpperCase();
			String reminderCount = currElement.getAttributeValue("reminderCount");
			String reminderDate = currElement.getAttributeValue("reminderDate");
			list.add(reminderCount);
			list.add(reminderDate);
			reminderMap.put(keyStr, list);
		}
	}
	/**
	 * Writes all currently stored data to XML
	 */
	public static boolean emailReminderIsEmpty(){
		return reminderMap.isEmpty();
	}
	public static boolean containKey(String keyS){
		return reminderMap.containsKey(keyS);
	}
	public static int getReminderCount(String keyS){
		ArrayList<String> tempA = reminderMap.get(keyS);
		return Integer.parseInt(tempA.get(0)); 
	}
	public static String getReminderDate(String keyS){
		return reminderMap.get(keyS).get(1); 
	}
	public static void putReminderInfo(String keyS){
		String reminderDate = ClockInterface.getTodayDate("MM/dd/yyyy");
		int reminderCount = 1;
		if (containKey(keyS)){
			reminderDate = getReminderDate(keyS);
			reminderCount = getReminderCount(keyS)+1;
		}
		ArrayList<String> list = new ArrayList<String>();
		list.add(Integer.toString(reminderCount));
		list.add(reminderDate);		
		reminderMap.put(keyS, list);
	}
	public static void writeToFile() throws ParseException{
		EmailNoticeWriter.write(reminderMap);
	}
}
